package com.bridge.utils;

import com.bridge.exception.BridgeProcessFailException;
import com.bridge.enums.BridgeErrorEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author Jay
 * @version v1.0
 * @description xml相关的工具类
 * @date 2018-12-28 10:36
 */
@Slf4j
public class TagUtils {

    private TagUtils() {
        throw new BridgeProcessFailException(BridgeErrorEnum.NOT_INSTANTIATION_ERROR);
    }

    /**
     * 注解或xml中的标签标识符开始符号
     */
    private static final String PREFIX = "@Bridge{";

    /**
     * 注解或xml中标签标识符结尾符号
     */
    private static final String SUFFIX = "}";


    /**
     * xml的tag校验
     *
     * @param key
     * @return
     */
    public static boolean isBridgeTag(String key) {
        if (StringUtils.isEmpty(key)) {
            throw new BridgeProcessFailException(BridgeErrorEnum.BNS_CHK_ERROR);
        }
        return key.startsWith(PREFIX) && key.endsWith(SUFFIX);
    }

    /**
     * 解析key
     *
     * @param key
     * @return
     */
    public static String keyParse(String key) {
        if (StringUtils.isEmpty(key)) {
            throw new BridgeProcessFailException(BridgeErrorEnum.BNS_CHK_ERROR);
        }
        return key.substring(PREFIX.length(), key.length() - SUFFIX.length());
    }
}
